package bma.common.hibernate;

import java.io.Serializable;

import bma.common.langutil.core.ObjectUtil;
import bma.common.langutil.core.ToStringUtil;

/**
 * 
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 */
public abstract class PersistenceObject implements Serializable {

	private static final long serialVersionUID = 1L;
	private transient int hashCode = Integer.MIN_VALUE;

	public PersistenceObject() {
		super();
	}

	public abstract Object getPrimaryKeyValue();

	public boolean equals(Object obj) {
		if (null == obj)
			return false;
		if (!(obj instanceof PersistenceObject))
			return false;

		PersistenceObject mObj = (PersistenceObject) obj;
		return ObjectUtil.equals(this.getPrimaryKeyValue(),
				mObj.getPrimaryKeyValue());

	}

	public int hashCode() {
		if (Integer.MIN_VALUE == this.hashCode) {
			Object pk = this.getPrimaryKeyValue();
			this.hashCode = null == pk ? super.hashCode() : pk.hashCode();
		}
		return this.hashCode;
	}

	@Override
	public String toString() {
		return ToStringUtil.fieldReflect(this);
	}

}
